var jQuery;
var wssh = {};


jQuery(function ($) {
    /*jslint browser:true */

    var status = $('#status'),
        btn = $('.btn-primary'),
        style = {};


    function parse_xterm_style() {
        var text = $('.xterm-helpers style').text();
        var arr = text.split('xterm-normal-char{width:');
        style.width = parseFloat(arr[1]);
        arr = text.split('div{height:');
        style.height = parseFloat(arr[1]);
    }


    function current_geometry() {
        if (!style.width || !style.height) {
            parse_xterm_style();
        }

        var cols = parseInt(window.innerWidth / style.width, 10) - 1;
        var rows = parseInt(window.innerHeight / style.height, 10);
        return {'cols': cols, 'rows': rows};
    }


    function resize_term(term, sock) {
        var geometry = current_geometry(),
            cols = geometry.cols,
            rows = geometry.rows;
        // console.log([cols, rows]);
        // console.log(term.geometry);

        if (cols !== term.geometry[0] || rows !== term.geometry[1]) {
            console.log('resizing term');
            term.resize(cols, rows);
            sock.send(JSON.stringify({'resize': [cols, rows]}));
        }
    }



    function callback(msg) {
        // console.log(msg);
        if (msg.status) {
            status.text(msg.status);
            setTimeout(function () {
                btn.prop('disabled', false);
            }, 3000);
            return;
        }
        // 将 http 替换成 ws
        var ws_url = window.location.href.replace('http', 'ws'),
            join = (ws_url[ws_url.length - 1] === '/' ? '' : '/'),
            url = ws_url + join + 'ws?id=' + msg.id,
            //新建一个 socket 连接！ 客户端 连接服务器  ws 是指没有加密的状态
            // 第二次 发送的 WebSocket(实际上是特殊的 http)
            sock = new window.WebSocket(url),
            encoding = msg.encoding,
            terminal = document.getElementById('#terminal'),
            term = new window.Terminal({
                cursorBlink: true,
            });

        console.log(url);
        console.log(encoding);
        wssh.sock = sock;
        wssh.term = term;

        term.on('data', function (data) {
            // console.log(data);
            sock.send(JSON.stringify({'data': data}));
        });

        sock.onopen = function () {
            $('.container').hide();
            term.open(terminal, true);
            term.toggleFullscreen(true);
        };
        // 返回数据 调用函数
        sock.onmessage = function (msg) {
            var reader = new window.FileReader();

            reader.onloadend = function () {
                var decoder = new window.TextDecoder(encoding);
                var text = decoder.decode(reader.result);
                // console.log(text);
                term.write(text);
                if (!term.resized) {
                    resize_term(term, sock);
                    term.resized = true;
                }
            };

            reader.readAsArrayBuffer(msg.data);
        };

        sock.onerror = function (e) {
            console.log(e);
        };

        sock.onclose = function (e) {
            console.log(e);
            term.destroy();
            wssh.term = undefined;
            wssh.sock = undefined;
            $('.container').show();
            status.text(e.reason);
            btn.prop('disabled', false);
        };
    }

    //入口
    $('form#connect').submit(function (event) {
        event.preventDefault();

        var form = $(this),
            // 获取当前对象属性的值
            url = form.attr('action'),
            //获取节点的 type 的值！
            type = form.attr('type'),
            //FormData对象，可以把所有表单元素的name与value组成一个queryString,提交到后台
            data = new FormData(this);

        if (!data.get('hostname') || !data.get('port') || !data.get('username')) {
            status.text('Hostname, port and username are required.');
            return;
        }

        var pk = data.get('privatekey');
        if (pk && pk.size > 16384) {
            status.text('Key size exceeds maximum value.');
            return;
        }

        status.text('');
        btn.prop('disabled', true);

        $.ajax({
            url: url,
            type: type,
            data: data,
            // 后端有相应数据，那么就会调用此函数！
            success: callback,
            cache: false,
            contentType: false,
            processData: false
        });

    });


    $(window).resize(function () {
        if (wssh.term && wssh.sock) {
            resize_term(wssh.term, wssh.sock);
        }
    });

});
